package com.example.billin.opengltest.shader.program

import android.content.Context
import android.opengl.GLES20
import com.example.billin.opengltest.ShaderHelper
import com.example.billin.opengltest.TextResourceReader

//主构造器是类头部的一部分，位于类名称之后
abstract class Program(context: Context, vertexResourceId: Int, shaderResourceId: Int) {
    protected val programId: Int = ShaderHelper.buildProgram(
        TextResourceReader.readTextFileFromResource(context, vertexResourceId),
        TextResourceReader.readTextFileFromResource(context, shaderResourceId)
    )

    fun useProgram() {
        GLES20.glUseProgram(programId)
    }

    abstract fun draw()
}


//这里定义的相当于静态类
inline fun <T : Program> T.apply(block: T.() -> Unit): T {
    useProgram()
    block()
    return this
}

inline fun <T : Program> T.draw(block: T.() -> Unit) {
    apply(block)
    draw()
}